﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using TpsAutoSurvey_PDA_Version_.RealizedClasses;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TpsAutoSurvey_PDA_Version_
{
    static class Program
    {
        [DllImport("coredll.Dll", SetLastError = true)]
        private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes, bool bInitialOwner, string lpName);

        [DllImport("coredll.Dll", SetLastError = true)]
        private static extern int ReleaseMutex(IntPtr hMutex);

        [StructLayout(LayoutKind.Sequential)]
        public class SECURITY_ATTRIBUTES
        {
            public int nLength;
            public int lpSecurityDescriptor;
            public int bInheritHandle;
        }

        const int ERROR_ALREADY_EXISTS = 0183; 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            try
            {

                IntPtr hMutex = CreateMutex(null, false, "StandardWorkMan");
                if (Marshal.GetLastWin32Error() != ERROR_ALREADY_EXISTS)
                {
                    int pId = Process.GetCurrentProcess().Id;
                    ToolClass.SetProfileString("ProcessId", pId.ToString());

                    Mfrm frm = new Mfrm();
                    new FrmPresenter(frm);
                    Application.Run(frm);

                }
                else
                {
                    ReleaseMutex(hMutex);
                    Application.Exit();
                }
            }
            catch
            {
                Application.Exit();
            }
        }
    }
}